<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品左边 -->
        <div class="goods-info-left">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <!-- 商品右边 -->
        <div class="goods-info-right">
          <!-- 名字区组件 -->
          <GoodsName :goods="goods"></GoodsName>
          <!-- 规格组件 -->
          <!-- :skuId="1369155864430120962" 为测试用的['蓝色','中国','10cm'] -->
          <GoodsSku :goods="goods" @change="getSku"></GoodsSku>
          <!-- 数量选择，max 最大值为商品库存 -->
          <XtxNumbox
            :max="goods.inventory"
            v-model="count"
            label="数量"
          ></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton type="primary" style="margin-top: 20px"
          @click="addToCartBtn "
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1"></GoodsHot>
          <GoodsHot :goodsId="goods.id" :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
  <div class="loading" v-else></div>
</template>

<script>
import { findGoods } from '@/api/product'
import GoodsRelevant from './components/goods-relevant.vue'
import { useRoute } from 'vue-router'
import { provide, ref, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsName,
    GoodsSales,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup () {
    const goods = ref(null)
    const count = ref(1)
    const route = useRoute()

    watch(
      () => route.params.id,
      (id) => {
        goods.value = null
        count.value = 1
        if (route.path === `/product/${id}`) {
          findGoods(id).then((res) => {
            // console.log(res)
            goods.value = res.result
          })
        }
      },
      { immediate: true }
    )

    const getSku = (val) => {
      console.log(val)
      if (val.skuId) {
        // 用于更新视图的，由于 goods 是通过 ref 定义的，所以要通过 goods.value 修改属性值
        goods.value.inventory = val.inventory
        goods.value.oldPrice = val.oldPrice
        goods.value.price = val.price
        // 存储当前 skuId，加入购物车的时候要用到
        goods.value.currSkuId = val.skuId
        goods.value.attrsText = val.attrsText
      } else {
        // 没有选择完整规格就重置为空
        goods.value.currSkuId = ''
      }
    }

    const store = useStore()

    // 加入购物车按钮
    const addToCartBtn = () => {
      // 判断是否选择了完整规格
      if (!goods.value.currSkuId) return Message({ type: 'warn', message: '请选择完整规格' })
      if (count.value > goods.value.inventory) return ({ type: 'warn', message: '库存不足了' })
      // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
      const cartItem = {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: goods.value.price,
        count: count.value,
        skuId: goods.value.currSkuId,
        attrsText: goods.value.attrsText,
        selected: true,
        nowPrice: goods.value.price,
        stock: goods.value.inventory || goods.value.stock, // 库存字段兼容性写法(库存在不同接口字段名有差别)
        isCollect: false,
        discount: 1,
        isEffective: true
      }
      // 编写mutaions添加购物车逻辑 - 修改state的函数
      store.dispatch('cart/insertCart', cartItem).then(res => {
        // console.log('加入购物车成功后提示', res)
        Message({ type: 'success', message: res })
      })
      console.log(goods, cartItem)
      // 编写actions进行添加操作 - 考虑到后期需要异步修改，所以再封装 actions
      // 在商品详情页实现添加逻辑
    }
    provide('goods', goods)
    return { goods, getSku, count, addToCartBtn }
  }
}
</script>

<style scoped lang='less'>
.loading {
  margin-top: 72px;
  min-height: 600px;
  background: #fff url(~@/assets/images/loading.gif) no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
// .goods-warn {
//   min-height: 600px;
//   background: #fff;
//   margin-top: 20px;
// }
</style>
